package view.bookAdmiView;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import dao.BorrowImfDao;
import model.BorrowImf;
import util.DbUtil;
import util.StringUtil;
import view.readerView.BorrowImfSearchInterFrm;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Color;

//JInternalFrame 提供很多本机窗体功能的轻量级对象，这些功能包括拖动、关闭、变成图标、调整大小、标题显示和支持菜单栏。
//通常，可将 JInternalFrame 添加到 JDesktopPane 中。UI 将特定于外观的操作委托给由 JDesktopPane 维护的 DesktopManager对象。


public class AllBorrowImfSearchInterFrm extends JInternalFrame {
	private JTextField s_userNameTxt;
	private JTextField s_bookNameTxt;
	private JTextField idTxt;
	private JTextField bookIdTxt;
	private JTextField bookNameTxt;
	private JTextField borrowTimeTxt;
	private JTextField renewNumTxt;
	private JTable borrowImfTable;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ButtonGroup buttonGroup_1 = new ButtonGroup();
	private JTextField returnTimeTxt;
	private JRadioButton borrowedJrb;
	private JRadioButton notBorrowedJrb ;
	
	private JLabel titleTxt;
	
	private DbUtil dbUtil=new DbUtil();
	private BorrowImfDao borrowImfDao=new BorrowImfDao();
	private JTextField userNameTxt;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {//EventQueue 是一个与平台无关的类，它将来自于底层同位体类和受信任的应用程序类的事件列入队列。 
//			public static void invokeLater(Runnable runnable)
//			导致 runnable 的 run 方法在 the system EventQueue 的指派线程中被调用
			public void run() {
				try {
					BorrowImfSearchInterFrm frame = new BorrowImfSearchInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AllBorrowImfSearchInterFrm() {
		getContentPane().setBackground(new Color(224, 255, 255));//getContentPane()  返回此内部窗体的内容窗格       
		setIconifiable(true);//setIconifiable(boolean b)设置 iconable 属性，对于能够使 JInternalFrame 变成一个图标的用户而言，该属性必须为 true
		setClosable(true);//setClosable(boolean b) 设置是否可以通过某个用户操作关闭此 JInternalFrame。       
		setTitle("借阅信息查询");
		setBounds(100, 100, 1790, 930);
		
		JPanel searchPanel = new JPanel();
		searchPanel.setBackground(new Color(230, 230, 250));
		searchPanel.setBorder(new TitledBorder(null, "\u501F\u9605\u8005\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel borrowImfpanel = new JPanel();
		borrowImfpanel.setBackground(new Color(230, 230, 250));
		borrowImfpanel.setBorder(new TitledBorder(null, "\u501F\u9605\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JScrollPane allBorrowImfscrollPane = new JScrollPane();
		
		titleTxt = new JLabel("借阅信息查询");
		titleTxt.setFont(new Font("华文楷体", Font.BOLD, 35));
		titleTxt.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/bookAdmi/图书馆借阅查询.png")));
		
//		GroupLayout 分组布局管理器。是一个 LayoutManager，它将组件按层次分组，以决定它们在 Container 中的位置。
//		GroupLayout 主要供生成器使用，但也可以手工编码。
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(624)
							.addComponent(titleTxt))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(25)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(borrowImfpanel, 0, 0, Short.MAX_VALUE)
								.addComponent(searchPanel, GroupLayout.DEFAULT_SIZE, 842, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
							.addComponent(allBorrowImfscrollPane, GroupLayout.PREFERRED_SIZE, 849, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(titleTxt)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(searchPanel, GroupLayout.PREFERRED_SIZE, 255, GroupLayout.PREFERRED_SIZE)
							.addGap(34)
							.addComponent(borrowImfpanel, GroupLayout.PREFERRED_SIZE, 455, GroupLayout.PREFERRED_SIZE))
						.addComponent(allBorrowImfscrollPane, GroupLayout.PREFERRED_SIZE, 735, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(24, Short.MAX_VALUE))
		);
		
		borrowImfTable = new JTable();
		borrowImfTable.setBackground(new Color(250, 235, 215));//设置表格背景颜色
		borrowImfTable.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));//设置表格开头字体
		borrowImfTable.getTableHeader().setBackground(new Color(127, 255, 212));//设置表格开头背景颜色
		borrowImfTable.setFont(new Font("楷体", Font.BOLD, 22));//设置表格内容字体
		borrowImfTable.setForeground(new Color(0,0,0));//设置表格内容字体颜色		
		borrowImfTable.setRowHeight(borrowImfTable.getRowHeight()+20);//设置表格内容的行高
		borrowImfTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent met) {
				borrowImfTableMousePressed(met);
				
			}
		});
//		public void setModel(TableModel dataModel)
//		将此表的数据模型设置为 newModel，并向其注册以获取来自新数据模型的侦听器通知。 
//		参数： dataModel - 此表的新数据源
		borrowImfTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u7528\u6237\u540D", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u7F16\u53F7", "\u501F\u9605\u72B6\u6001", "\u501F\u51FA\u65F6\u95F4", "\u5E94\u8FD8\u65F6\u95F4", "\u7EED\u501F\u6B21\u6570"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		//setViewportView(Component view)创建一个视口（如果有必要）并设置其视图。
		//不直接为 JScrollPane 构造方法提供视图的应用程序应使用此方法指定将显示在滚动窗格中的滚动组件子级。
		allBorrowImfscrollPane.setViewportView(borrowImfTable);
		
		JLabel id = new JLabel("编    号:");
		id.setFont(new Font("宋体", Font.BOLD, 23));
		id.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		idTxt = new JTextField();
		idTxt.setEditable(false);
		idTxt.setFont(new Font("宋体", Font.BOLD, 23));
		idTxt.setColumns(10);
		
		JLabel bookId = new JLabel("图书编号：");
		bookId.setFont(new Font("宋体", Font.BOLD, 23));
		bookId.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		bookIdTxt = new JTextField();
		bookIdTxt.setEditable(false);
		bookIdTxt.setFont(new Font("宋体", Font.BOLD, 23));
		bookIdTxt.setColumns(10);
		
		JLabel bookName = new JLabel("图书名称：");
		bookName.setFont(new Font("宋体", Font.BOLD, 23));
		bookName.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setEditable(false);
		bookNameTxt.setFont(new Font("宋体", Font.BOLD, 23));
		bookNameTxt.setColumns(10);
		
		JLabel borrowTime = new JLabel("借出时间：");
		borrowTime.setFont(new Font("宋体", Font.BOLD, 23));
		borrowTime.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		borrowTimeTxt = new JTextField();
		borrowTimeTxt.setEditable(false);
		borrowTimeTxt.setFont(new Font("宋体", Font.BOLD, 23));
		borrowTimeTxt.setColumns(10);
		
		JLabel returnTime = new JLabel("应还时间：");
		returnTime.setFont(new Font("宋体", Font.BOLD, 23));
		returnTime.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		JLabel status = new JLabel("借阅状态：");
		status.setFont(new Font("宋体", Font.BOLD, 23));
		status.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		renewNumTxt = new JTextField();
		renewNumTxt.setEditable(false);
		renewNumTxt.setFont(new Font("宋体", Font.BOLD, 23));
		renewNumTxt.setColumns(10);
		
		JLabel renewNum = new JLabel("续借次数：");
		renewNum.setFont(new Font("宋体", Font.BOLD, 23));
		renewNum.setIcon(new ImageIcon(BorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		
		borrowedJrb = new JRadioButton("已借");
		borrowedJrb.setEnabled(false);
		borrowedJrb.setFont(new Font("宋体", Font.BOLD, 23));
		buttonGroup.add(borrowedJrb);
		
		notBorrowedJrb = new JRadioButton("在馆");
		notBorrowedJrb.setEnabled(false);
		notBorrowedJrb.setFont(new Font("宋体", Font.BOLD, 23));
		buttonGroup.add(notBorrowedJrb);
		notBorrowedJrb.setSelected(true);
		
		returnTimeTxt = new JTextField();
		returnTimeTxt.setEditable(false);
		returnTimeTxt.setFont(new Font("宋体", Font.BOLD, 23));
		returnTimeTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("续借");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				renewActionPerformed(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/readerAdim/续借.png")));
		btnNewButton.setFont(new Font("宋体", Font.BOLD, 25));
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reSetActionPerformed(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/bookAdmi/取消.png")));
		btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 25));
		
		JLabel userName = new JLabel("用户名：");
		userName.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/book.png")));
		userName.setFont(new Font("宋体", Font.BOLD, 23));
		
		userNameTxt = new JTextField();
		userNameTxt.setEditable(false);
		userNameTxt.setFont(new Font("宋体", Font.BOLD, 23));
		userNameTxt.setColumns(10);
		//以下是对borrowPanel面板里的组件设置布局
		GroupLayout gl_borrowImfpanel = new GroupLayout(borrowImfpanel);
		gl_borrowImfpanel.setHorizontalGroup(
			gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_borrowImfpanel.createSequentialGroup()
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addComponent(id)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addGap(2)
							.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_borrowImfpanel.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(renewNum)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(renewNumTxt, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
								.addGroup(gl_borrowImfpanel.createSequentialGroup()
									.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(bookId, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(borrowTime, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.TRAILING)
										.addComponent(borrowTimeTxt, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
										.addComponent(bookIdTxt, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE))))))
					.addPreferredGap(ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
						.addComponent(userName)
						.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.TRAILING)
							.addComponent(status)
							.addComponent(bookName)
							.addComponent(returnTime)))
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addGap(12)
							.addComponent(notBorrowedJrb)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(borrowedJrb))
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
								.addComponent(userNameTxt)
								.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING, false)
									.addComponent(returnTimeTxt)
									.addComponent(bookNameTxt, GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)))))
					.addGap(80))
				.addGroup(gl_borrowImfpanel.createSequentialGroup()
					.addGap(143)
					.addComponent(btnNewButton)
					.addGap(134)
					.addComponent(btnNewButton_1)
					.addContainerGap(331, Short.MAX_VALUE))
		);
		gl_borrowImfpanel.setVerticalGroup(
			gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_borrowImfpanel.createSequentialGroup()
					.addGap(32)
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(id)
						.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
						.addComponent(borrowedJrb)
						.addComponent(notBorrowedJrb)
						.addComponent(status))
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addGap(40)
							.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(bookIdTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
								.addComponent(bookId)))
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addGap(30)
							.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(bookName)
								.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))))
					.addGap(31)
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_borrowImfpanel.createSequentialGroup()
							.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(borrowTime)
								.addComponent(borrowTimeTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
								.addComponent(returnTime))
							.addGap(37)
							.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(renewNum)
								.addComponent(renewNumTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
								.addComponent(userName)
								.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)))
						.addComponent(returnTimeTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(gl_borrowImfpanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(34))
		);
		borrowImfpanel.setLayout(gl_borrowImfpanel);
		
		JLabel s_userName = new JLabel("用户名：");
		s_userName.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		s_userName.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/bookAdmi/用户.png")));
		
		s_userNameTxt = new JTextField();
		s_userNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_userNameTxt.setColumns(10);
		
		JLabel s_bookName = new JLabel("图书名称：");
		s_bookName.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		s_bookName.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/bookAdmi/密码.png")));
		
		s_bookNameTxt = new JTextField();
		s_bookNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_bookNameTxt.setColumns(10);
		
		JButton searchButton = new JButton("查询");
		searchButton.setFont(new Font("楷体", Font.BOLD, 30));
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowImfSearchActionPerformed(e);
			}
		});
		searchButton.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/bookAdmi/查询.png")));
		
		JButton cancerButton = new JButton("取消");
		cancerButton.setFont(new Font("楷体", Font.BOLD, 30));
		cancerButton.setIcon(new ImageIcon(AllBorrowImfSearchInterFrm.class.getResource("/images/bookAdmi/取消.png")));
		cancerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reSetValueActionPerformed(e);
			}
		});
		GroupLayout gl_searchPanel = new GroupLayout(searchPanel);
		gl_searchPanel.setHorizontalGroup(
			gl_searchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_searchPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(s_userName)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_userNameTxt, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
					.addComponent(s_bookName)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_bookNameTxt, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
					.addGap(77))
				.addGroup(gl_searchPanel.createSequentialGroup()
					.addGap(157)
					.addComponent(searchButton)
					.addGap(66)
					.addComponent(cancerButton)
					.addContainerGap(325, Short.MAX_VALUE))
		);
		gl_searchPanel.setVerticalGroup(
			gl_searchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_searchPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_searchPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(s_userName)
						.addComponent(s_userNameTxt, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_bookNameTxt, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_bookName))
					.addPreferredGap(ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
					.addGroup(gl_searchPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(cancerButton)
						.addComponent(searchButton))
					.addGap(44))
		);
		searchPanel.setLayout(gl_searchPanel);
		getContentPane().setLayout(groupLayout);
		//设置JFrame居中显示
		this.setLocation(80, 0);
	
		this.fillTable(new BorrowImf());

		
	}

	
	
	/**
	 * 重置借阅表单
	 */
	private void reSetActionPerformed(ActionEvent e) {
		this.idTxt.setText("");
		this.bookNameTxt.setText("");
		this.bookIdTxt.setText("");		
		this.notBorrowedJrb.setSelected(true);				
		this.borrowTimeTxt.setText("");
		this.returnTimeTxt.setText("");
		this.renewNumTxt.setText("");
	}
	
	
	/**
	 * 续借事件处理
	 * 续借。借阅者可以通过网络或图书管理员续借图书2次，每次续借1个月。
	 * @param e
	 */
	private void renewActionPerformed(ActionEvent evt) {
		if(StringUtil.isEmpty(this.idTxt.getText())) {
			JOptionPane.showMessageDialog(null, "请选择要查看的记录!");
		}
		else {
			int id=Integer.parseInt(this.idTxt.getText());				
			int renewNum=Integer.parseInt(this.renewNumTxt.getText());
			int row=this.borrowImfTable.getSelectedRow();
			java.sql.Date returnTime2;		
			String returnTime1=(String)borrowImfTable.getValueAt(row,5);
			String status="";
			if(notBorrowedJrb.isSelected()) {
				status="在馆";
				
			}
			else if(borrowedJrb.isSelected()) {
				status="已借";
			}
			if(renewNum<2) {
				if(status=="已借") {
					renewNum=renewNum+1;
					//修改还书时间为一个月后
					
					String d1=this.rd(returnTime1);
					returnTime2=java.sql.Date.valueOf(d1);
				}
				else {
					returnTime2=java.sql.Date.valueOf(returnTime1);
					JOptionPane.showMessageDialog(null, "本图书状态为在馆，请正确选择图书");
					return;
				}
				
			}
			else {
				returnTime2=java.sql.Date.valueOf(returnTime1);
				JOptionPane.showMessageDialog(null, "续借次数已达到上限，不允许再次续借");
				return;
			}
			BorrowImf borrowImf=new BorrowImf(id,returnTime2,renewNum);
			Connection con=null;			
			try {
				con=dbUtil.getCon();
				int addNum=borrowImfDao.update2(con, borrowImf);
				if(addNum==1) {
					JOptionPane.showMessageDialog(null, "续借成功!");
					this.reSetValue2();
					this.fillTable(new BorrowImf());
					
					
				}
				else {
					JOptionPane.showMessageDialog(null, "续借失败!");				
					
				}
				
			}catch(Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "续借失败!");		
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {					
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "续借失败");
				}
			}
		}
		
	}
	

	/**
	 * 设置续借成功后还书日期推迟一个月
	 * @param d
	 * @return
	 */
	private String rd(String d){
		Calendar c = Calendar.getInstance();//获得一个日历的实例
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		try{
		date = sdf.parse(d);//初始日期
		}catch(Exception e){
		}
		c.setTime(date);//设置日历时间
		c.add(Calendar.MONTH,1);//在日历的月份上增加1个月
		return sdf.format(c.getTime());
		}
	/**
	 * 表单点击事件处理
	 * @param met
	 */
	private void borrowImfTableMousePressed(MouseEvent met) {
		int row=this.borrowImfTable.getSelectedRow();
		this.idTxt.setText((String)borrowImfTable.getValueAt(row, 0));
		this.userNameTxt.setText((String)borrowImfTable.getValueAt(row, 1));
		this.bookNameTxt.setText((String)borrowImfTable.getValueAt(row, 2));		
		this.bookIdTxt.setText((String)borrowImfTable.getValueAt(row, 3));
		String status=(String)borrowImfTable.getValueAt(row, 4);
		if("在馆".equals(status)) {
			this.notBorrowedJrb.setSelected(true);
		}else if("已借".equals(status)) {
			this.borrowedJrb.setSelected(true);
		}	
		this.borrowTimeTxt.setText((String)borrowImfTable.getValueAt(row, 5));
		this.returnTimeTxt.setText((String)borrowImfTable.getValueAt(row, 6));
		this.renewNumTxt.setText((String)borrowImfTable.getValueAt(row, 7));
		if(status.equalsIgnoreCase("已借")) {
			String returnTime2=(String)borrowImfTable.getValueAt(row, 6);
			
			try {
				if(StringUtil.compareDate(returnTime2)) {
					JOptionPane.showMessageDialog(null, "该书已超过规定借阅时间，请按规定收取罚款金额后归还图书");
				}
			} catch (HeadlessException e) {				
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}

			
		}
	}

	/**
	 * 借阅信息查询事件处理
	 * @param e
	 */
	private void borrowImfSearchActionPerformed(ActionEvent e) {
		String userName=this.s_userNameTxt.getText();
		String bookName=this.s_bookNameTxt.getText();
		String status="已借";
		BorrowImf borrowImf=new BorrowImf(userName,bookName,status);		
		this.fillTable(borrowImf);		
	}

	/**
	 * 初始化表格数据
	 * @param book
	 */
	private void fillTable(BorrowImf borrowImf) {
		DefaultTableModel dtm=(DefaultTableModel) borrowImfTable.getModel();
		dtm.setRowCount(0);//设置成0行
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=borrowImfDao.list2(con, borrowImf);
			while(rs.next()) {
				Vector v=new Vector();
				v.add(rs.getString("id"));
				v.add(rs.getString("b_userName"));
				v.add(rs.getString("b_bookName"));
				v.add(rs.getString("bookId"));
				v.add(rs.getString("b_status"));
				v.add(rs.getString("borrowTime"));
				v.add(rs.getString("returnTime"));
				v.add(rs.getString("renewNum"));				
				dtm.addRow(v);
				
			}
		}catch(Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "请输入正确信息");
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
		
	
	
	
	/**
	 * 重置表单
	 */
	private void reSetValueActionPerformed(ActionEvent e) {
		this.reSetValue();
	}
	
	private void reSetValue() {
		this.s_bookNameTxt.setText("");
		this.s_userNameTxt.setText("");
	}
	
	//续借成功后重置表格
		private void reSetValue2() {
			this.idTxt.setText("");
			this.bookIdTxt.setText("");
			this.bookNameTxt.setText("");
			this.borrowTimeTxt.setText("");
			this.returnTimeTxt.setText("");
			this.renewNumTxt.setText("0");
			
			
			
		}
	
}
